﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindStyle.SPEntity.Attributes;

namespace WindStyle.SPEntity
{
    public class Entity
    {
        public int SPListItemId { get; internal set; }
        public Guid SPListItemUniqueId { get; private set; }
        public Guid SPParentListId { get; private set; }
        public Guid SPParentWebId { get; private set; }
        public Guid SPParentSiteId { get; private set; }

        public virtual string Title { get; set; }

        public virtual void Read(SPListItem item, SPRegionalSettings regionalSettings = null)
        {
            this.SPListItemId = item.ID;
            try
            {
                this.SPListItemUniqueId = item.UniqueId;
            }
            catch { }
            this.SPParentListId = item.ParentList.ID;
            this.SPParentWebId = item.Web.ID;
            this.SPParentSiteId = item.Web.Site.ID;

            this.Title = item.Title;

            MappingHelper.PopulateEntity(item, this, regionalSettings);
        }

        public virtual void Write(SPListItem item, SPRegionalSettings regionalSettings = null)
        {
            item[SPBuiltInFieldId.Title] = this.Title;
            MappingHelper.PopulateListItem(item, this, regionalSettings);
        }
    }
}
